# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import re

import scrapy
from copy import deepcopy
from scrapy import Request
from scrapy.exceptions import DropItem
from scrapy.pipelines.images import ImagesPipeline


class BobopicPipeline(object):
    def process_item(self, item, spider):
        return item


class PicsDownloadPipeline(ImagesPipeline):
    def get_media_requests(self, item, info):
        print("get_media_requests")
        for image_url in item['image_urls']:
            yield scrapy.Request(
                image_url,
                meta={"item":deepcopy(item)}
            )

    def item_completed(self, results, item, info):
        print("item_completed")
        # 将下载的图片路径（传入到results中）存储到 image_paths 项目组中，如果其中没有图片，我们将丢弃项目:
        # print("item",item)
        # print("results",results)
        # print("info", info)
        image_path = [x['path'] for ok, x in results if ok]
        # print("image_path", image_path)
        if not image_path:
            raise DropItem("Item contains no images")
        return item

    def file_path(self, request, response=None, info=None):
        # 接收上面meta传递过来的图片名称
        print("file_path")
        item = request.meta['item']
        return str(item["file_name"]) + "/" + str(item["title"]) + ".jpg"
